package logic

import (
	"context"
	"fmt"
	"goodsrpc/cache"
	"goodsrpc/mysql"

	"goodsrpc/goods"
	"goodsrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateImgLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateImgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateImgLogic {
	return &UpdateImgLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateImgLogic) UpdateImg(in *goods.UpdateImgRequest) (*goods.UpdateImgResponse, error) {
	// todo: add your logic here and delete this line
	key := "send_sms" + in.Mobile
	result, err := cache.Rdb.Get(l.ctx, key).Result()
	if err != nil {
		return nil, err
	}
	if result != in.Code {
		logx.Info("短信验证码错误")
		return nil, fmt.Errorf("短信验证码错误")
	}
	var u mysql.User
	err = u.UpdateImg(int(in.UserId), in.Img)
	if err != nil {
		logx.Info("修改图像失败")
		return nil, err
	}

	return &goods.UpdateImgResponse{
		Id: int64(u.ID),
	}, nil
}
